class Solution {
public:
    bool isBipartite(vector<vector<int>>& graph) {
int i,n,m = graph.size();
 vector<int> t(m, 0);
        queue<int> p;   
        if (m == 0) return true;        
        for (i = 0; i < m; i++) {
            if (!t[i]) {
                p.push(i);
                t[i] = 1;
            }
            while (!p.empty()) {
                n = p.front();
                p.pop();
                for (auto& j : graph[n]) {
                    if (!t[j]) {
                        t[j] = (t[n] == 1 ? 2 : 1);
                        p.push(j);
                    } else if (t[j] == t[n]) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
};